//
//  ToastView.swift
//  Edaixi-iOS
//
//  Created by 刘树华 on 2017/11/29.
//  Copyright © 2017年 刘树华. All rights reserved.
//

import Foundation
import UIKit

let ToastPositionTop = "CSToastPositionTop"
let ToastPositionCenter = "CSToastPositionCenter"
let ToastPositionBottom = "CSToastPositionBottom"




class ToastStyle: NSObject {
    
    var backgroundColor = UIColor.colorWithRGBA(r: 0, g: 0, b: 0, a: 0.8)
    var titleColor = UIColor.white
    var messageColor = UIColor.white
    var maxWidthPercentage : CGFloat = 0.8 {
        willSet {
            self.maxWidthPercentage =  max(min(newValue, 1.0), 0.0)
        }
    }
    var maxHeightPercentage : CGFloat = 0.8{
        willSet{
            self.maxHeightPercentage = max(min(newValue, 1.0), 0.0)
        }
    }
    var horizontalPadding : CGFloat = 10.0
    var verticalPadding : CGFloat = 10.0
    var cornerRadius : CGFloat = 10.0
    var titleFont = UIFont.boldSystemFont(ofSize: 16)
    var messageFont = UIFont.systemFont(ofSize: 16)
    var titleAlignment = NSTextAlignment.center
    var messageAlignment = NSTextAlignment.left
    var titleNumberOfLines = 0
    var messageNumberOfLines = 0
    var activitySize = CGSize(width: 100, height: 100)
    var fadeDuration = 0.2
   
}


class ToastStyleManager: NSObject {
    
    static let Shared = { ()->ToastStyleManager in
        let manager = ToastStyleManager()
        manager.sharedStyle = ToastStyle()
        return manager
    }()
    var sharedStyle : ToastStyle!
    
    
}

class ToastView:UIView  {
    
    func setUpToast (title:String?=nil,message:String?=nil,style:ToastStyle=ToastStyleManager.Shared.sharedStyle,des:UIView,rotation:CGAffineTransform?)  {
        if message == nil && title == nil {
            return
        }
        var titleLabel : UILabel!
        var messageLabel : UILabel!
        
        
        self.autoresizingMask = UIViewAutoresizing.flexibleBottomMargin.union(.flexibleTopMargin).union(.flexibleLeftMargin).union(.flexibleRightMargin)
        self.layer.cornerRadius = style.cornerRadius
        self.backgroundColor = style.backgroundColor
        if title != nil {
            titleLabel = UILabel()
            titleLabel.numberOfLines = style.titleNumberOfLines
            titleLabel.font = style.titleFont
            titleLabel.textAlignment = style.titleAlignment
            titleLabel.lineBreakMode = .byTruncatingTail
            titleLabel.textColor = style.titleColor
            titleLabel.backgroundColor = UIColor.clear
            if rotation != nil{
                titleLabel.transform = rotation!
            }
            let maxSize = CGSize(width: des.bounds.width*style.maxWidthPercentage, height: des.bounds.height*style.maxHeightPercentage)
            let fitSize = titleLabel.sizeThatFits(maxSize)
            let  expectedSize = CGSize(width: min(maxSize.width, fitSize.width), height: min(maxSize.height, fitSize.height))
            titleLabel.frame = CGRect(x: 0, y: 0, width: expectedSize.width, height: expectedSize.height)
        }
        if message != nil {
            messageLabel = UILabel()
//            messageLabel.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
            messageLabel.numberOfLines = style.messageNumberOfLines
            messageLabel.font = style.messageFont
            messageLabel.textAlignment = style.messageAlignment
            messageLabel.lineBreakMode = .byTruncatingTail
            messageLabel.textColor = style.messageColor
            messageLabel.backgroundColor = UIColor.clear
            messageLabel.text = message
            if rotation != nil{
                messageLabel.transform = rotation!
            }
            let maxSize = CGSize(width: des.bounds.width*style.maxWidthPercentage, height: des.bounds.height*style.maxHeightPercentage)
            let fitSize = messageLabel.sizeThatFits(maxSize)
            let  expectedSize = CGSize(width: min(maxSize.width, fitSize.width), height: min(maxSize.height, fitSize.height))
            messageLabel.frame = CGRect(x: 0, y: 0, width: expectedSize.width, height: expectedSize.height)
            
        }
        var titleRect = CGRect.zero
        if titleLabel != nil {
            titleRect.origin.x = style.horizontalPadding
            titleRect.origin.y = style.verticalPadding
            titleRect.size.width = titleLabel.bounds.width
            titleRect.size.height = titleLabel.bounds.height
        }
        
        var messageRect = CGRect.zero
        if messageLabel != nil {
            messageRect.origin.x = style.horizontalPadding
            messageRect.origin.y = style.verticalPadding
            messageRect.size.width = messageLabel.bounds.width
            messageRect.size.height = messageLabel.bounds.height
        }
        let maxWidth = max(titleRect.width, messageRect.width)
        let maxX = max(titleRect.origin.x, messageRect.origin.x)
        
        let wraperWidth = max(style.horizontalPadding*2, maxX+maxWidth+style.horizontalPadding)
        let wraperHeight = max(messageRect.origin.y+messageRect.size.height+style.verticalPadding, style.verticalPadding*2)
        self.frame = CGRect(x: 0, y: 0, width: wraperWidth, height: wraperHeight)
        
        if titleLabel != nil {
            titleLabel.frame = titleRect
            self.addSubview(titleLabel)
        }
        if messageLabel != nil {
            messageLabel.frame = messageRect
            self.addSubview(messageLabel)
        }
    }
    
}
